package com.zuipin.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zuipin.entity.BaseProductBrand;
import com.zuipin.framework.emuns.ErrorCode;
import com.zuipin.framework.result.PageResult;
import com.zuipin.mapper.BaseProductBrandMapper;
import com.zuipin.service.IBaseProductBrandService;
import com.zuipin.util.DateUtils;
import com.zuipin.util.Pagination;
import com.zuipin.util.StringUtil;
import com.zuipin.vo.ProductBrandVo;

/**
 * 商品品牌业务类实现
 *
 * @ClassName BaseProductBrandServiceImpl
 * @author wangcongyi
 * @date 2017年7月18日 上午11:48:29
 */

@Service
public class BaseProductBrandServiceImpl implements IBaseProductBrandService {
	
	@Resource
	private BaseProductBrandMapper baseProductBrandMapper;
	
	@Override
	public List<BaseProductBrand> selectProductBrand() {
		return baseProductBrandMapper.selectProductBrand();
	}
	
	@Override
	public BaseProductBrand selectByPrimaryKey(Short id) {
		return baseProductBrandMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public List<BaseProductBrand> selectProductBrandByName(String brandName, Pagination page) {
		
		return baseProductBrandMapper.selectProductBrandByName(brandName, page);
	}
	
	@Override
	public int insertProductBrand(String brandName, String logoUrl, String remark) {
		return baseProductBrandMapper.insertProductBrand(brandName, logoUrl, remark);
	}
	
	@Override
	public int updateProductBrand(Long id, String brandName, String logoUrl, String remark) {
		return baseProductBrandMapper.updateProductBrand(id, brandName, logoUrl, remark);
	}
	
	@Override
	public int insert(BaseProductBrand baseProductBrand) {
		baseProductBrand.setCreateTime(DateUtils.getCurrentDateTime());
		baseProductBrand.setUpdateTime(DateUtils.getCurrentDateTime());
		return baseProductBrandMapper.insert(baseProductBrand);
	}
	
	@Override
	public int update(BaseProductBrand baseProductBrand) {
		baseProductBrand.setUpdateTime(DateUtils.getCurrentDateTime());
		return baseProductBrandMapper.update(baseProductBrand);
	}
	
	/** 
	 *
	 * @Title: findById 
	 * @author: wuyicheng
	 * @date: 2017年8月15日下午2:24:55 
	 * @param id
	 * @return
	 * @throws 
	 */
	@Override
	public BaseProductBrand findById(Long id) {
		return baseProductBrandMapper.findById(id);
	}
	
	/** 
	 * @Title: selectProductBrand 
	 * @Description: 后台查询品牌信息
	 * @author xjd
	 * @param @param baseProductBrand
	 * @param @param pageNumber
	 * @param @param pageSize
	 * @param @return
	 * @throws 
	 */
	@Override
	public PageResult<List<BaseProductBrand>> selectProductBrand(BaseProductBrand baseProductBrand, Integer pageNumber, Integer pageSize) {
		PageResult<List<BaseProductBrand>> pageObjectVo = new PageResult<>();
		if (baseProductBrand != null) {
			if (!StringUtil.isBlank(baseProductBrand.getCreateTime())) {
				
				String createTime = baseProductBrand.getCreateTime();
				baseProductBrand.setCreateTime(createTime + DateUtils.BEGIN_TIME);
				baseProductBrand.setEndTime(createTime + DateUtils.END_TIME);
			}
			PageHelper.startPage(pageNumber, pageSize);
			List<BaseProductBrand> baseProductBrandList = baseProductBrandMapper.selectProductBrandBack(baseProductBrand);
			PageInfo<BaseProductBrand> page = new PageInfo<>(baseProductBrandList);
			pageObjectVo.setData(baseProductBrandList);
			pageObjectVo.setTotal_record(Integer.valueOf(page.getTotal() + ""));
			pageObjectVo.setErrorCode(ErrorCode.SUCCESS);
			return pageObjectVo;
		}
		pageObjectVo.setData(null);
		pageObjectVo.setTotal_record(0);
		pageObjectVo.setErrorCode(ErrorCode.SUCCESS);
		return pageObjectVo;
	}
	
	/** 
	 *
	 * @Title: findListByIds 
	 * @author: wuyicheng
	 * @date: 2017年8月18日下午9:11:53 
	 * @param sysId
	 * @param brandIds
	 * @return
	 * @throws 
	 */
	@Override
	public List<ProductBrandVo> findListByIds(Long sysId, List<Long> brandIds) {
		return baseProductBrandMapper.selectProductBrandByIds(brandIds, sysId, BaseProductBrand.IS_ACTIVE_YES_1);
	}
}
